using System;
using System.Linq;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class resetTeamTurn : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        public int TeamID { get; set; }
        public resetTeamTurn(HttpContext context)
            : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            try
            {
                for (int i = 1; i <= 8; i++)
                {
                    TeamsTurn r = (from x in dbContext.TeamsTurns where x.TeamID == TeamID && x.ResourceID == i select x).FirstOrDefault();
                    
                    r.Value = 0;
                           
                    dbContext.SaveChanges();
                }
                return "OK";
            }
            catch (Exception e)
            {
                return "ERROR";
            }
        }

        public override void InitializeProperties()
        {
            try
            {
                TeamID = Convert.ToInt32(Parameters["teamid"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}